package com.xdxc.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdxc.model.dto.ExamRecordDTO;
import com.xdxc.model.entity.ExamRecord;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface ExamRecordMapper extends BaseMapper<ExamRecord> {

    @Select("SELECT er.id, " +
            "e.title AS examName, " +
            "e.start_time AS startTime, " +
            "e.end_time AS endTime, " +
            "er.obj_score AS objectiveScore, " +
            "er.subj_score AS subjectiveScore, " +
            "er.total_score AS totalScore, " +
            "e.total_score AS paperTotalScore, " +
            "er.passed AS isPassed " +
            "FROM exam_record er " +
            "JOIN exams e ON er.exam_id = e.id " +
            "WHERE er.user_id = #{userId} " +
            "ORDER BY er.create_time DESC")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "examName", column = "examName"),
            @Result(property = "startTime", column = "startTime"),
            @Result(property = "endTime", column = "endTime"),
            @Result(property = "objectiveScore", column = "objectiveScore",
                    javaType = BigDecimal.class),
            @Result(property = "subjectiveScore", column = "subjectiveScore",
                    javaType = BigDecimal.class),
            @Result(property = "totalScore", column = "totalScore",
                    javaType = BigDecimal.class),
            @Result(property = "paperTotalScore", column = "paperTotalScore",
                    javaType = BigDecimal.class),
            @Result(property = "isPassed", column = "isPassed")
    })
    Page<ExamRecordDTO> findExamRecordsByUserId(Page<ExamRecordDTO> page, @Param("userId") Long userId);
}